// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import config from "../config/config";
import AudioManage from "../manage/AudioManage";
import checkPoint = require("../configure/t_checkPoint");
import batching = require("../configure/t_batching");
import vegetableType = require("../configure/t_vegetableType");
import dialogue = require("../configure/t_dialogue");
import guide = require("../configure/t_guide");
import zs_sdk = require("../configure/zs.sdk");

import StartGame from "./StartGame";
import AdManage from "../manage/AdManage";
import func from "../config/func";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Node)
    m_start: cc.Node = null;
    @property(cc.Label)
    m_loading: cc.Label = null;

    onLoad() {
        config.openPopup = false;
        if (config.firstStartGame) {
            config.firstStartGame = false;
            this.readConfigure();
            if (config.guide) {
                localStorage.clear();
            }
            if (config.gameType == "wx") {
                window["wx"].showShareMenu({
                    withShareTicket: true
                })
            } else if (config.gameType == "vivo") {
                config.zs_sdk.login((user_id: any) => {
                    console.log("sdk登录成功", user_id);
                }, (res: any) => {
                    console.log(res, "sdk登录失败");
                })
            }
            cc.game.on(cc.game.EVENT_HIDE, () => {
                console.log("进入后台");
                AudioManage.audioManage.stopBgAudio();
            })
            cc.game.on(cc.game.EVENT_SHOW, () => {
                console.log("进入前台");
                AudioManage.audioManage.resumeBgAudio();
            })
            AudioManage.audioManage.playBgMusic(config.soundPath.bg, true);
            AudioManage.audioManage.setBgAudioVolume();
            AudioManage.audioManage.setEffectVolume();
        } else {
            AdManage.instance.init();
            if (config.gameType == "vivo") {
                setTimeout(() => {
                    func.creatVivoNativeAd();
                }, 200);
            } else if (config.gameType == "QQ" && config.qq_loadCfg && config.qq_loadCfg.mistouchOn == 1) {
                AdManage.instance.initQQcreateAppBox();
            }
        }
        cc.director.preloadScene("game", (completedCount: number, totalCount: number, item: any) => {
            // this.spr_completedCount.fillRange = completedCount / totalCount;
        }, (err: any) => {
            if (err) {
                console.log(err, "加载游戏界面失败");
            }
            if (config.gameType == "vivo" && config.qg_loadCfg == null) {
                config.zs_sdk.loadCfg((res: any) => {
                    config.qg_loadCfg = res;
                    console.log(JSON.stringify(res), "导出为开关");
                    this.changeInfacae();
                }, (err: any) => {
                    console.log(JSON.stringify(err), "导出为开关获取失败");
                    this.changeInfacae();
                })
            } else if (config.gameType == "QQ") {
                moosnow.http.getAllConfig((res) => {
                    config.qq_loadCfg = res;
                    this.changeInfacae();
                })
                setTimeout(() => {
                    if (this.node && this.m_loading.node.active) {
                        this.changeInfacae();
                    }
                }, 7000);
            } else {
                this.changeInfacae();
            }
            // this.changeInfacae();
        })

    }

    changeInfacae() {
        this.m_loading.node.active = false;
        this.m_start.active = true;
        this.m_start.getComponent(StartGame).init();
    }

    /**
     * 读取配置表
     */
    readConfigure() {
        config.dataJson.checkPoint = checkPoint.t_checkPoint;
        config.dataJson.batching = batching.t_batching;
        config.dataJson.vegetableType = vegetableType.t_vegetableType;
        config.dataJson.dialogue = dialogue.t_dialogue;
        config.dataJson.guide = guide.t_guide;
        if (config.gameType == "vivo") {
            config.zs_sdk = zs_sdk;
        }
        console.log(config.dataJson);
    }
}
